﻿package net.wangit.adm.org;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.DB;
import net.wangit.support.action.TransactionAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.org.OrgCache;
import net.wangit.util.Checker;

public class DeleteOrgAction extends TransactionAction {

	private static final String CONFIRM = "select * from AORG where parentid=?";
	private static final String ORGANIZATION_DELETE = "delete from AORG where orgid=?";
	private static final String ADMINISTRATOR_DELETE = "delete from AADMINORG where orgid=?";
	private static final String ROLE_DELETE = "delete from AORGROLE where orgid=?";
	private static final String USER_DELETE = "update AUSER set orgid='' where orgid=?";
	private static final String LEVEL_DELETE = "delete from AORGLEVEL where upperid=? or lowerid=? ";

	public DeleteOrgAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String orgId = ac.getRequestParameter("orgid");
		net.wangit.MapList list = db.query(
				"select * from AORG where parentid=?", orgId, 2);
		if (!Checker.isEmpty(list)) {
			ac.getActionResult().setSuccessful(false);
			ac.getActionResult().addError(
					LabelUtil.get(ac, "adm", "org.has_subnodes", orgId));
		} else {
			db.execute("delete from AORG where orgid=?", orgId, 2);
			db.execute("delete from AADMINORG where orgid=?", orgId, 2);
			db.execute("delete from AORGROLE where orgid=?", orgId, 2);
			db.execute("update AUSER set orgid='' where orgid=?", orgId, 2);
			db.execute("delete from AORGLEVEL where upperid=? or lowerid=? ",
					new String[] { orgId, orgId }, new int[] { 2, 2 });
			OrgCache.clear(orgId);
			AdminCache.clearAll();
		}
	}
}
